package com.dsjy.yf.address.utils;

import com.dsjy.yf.address.entity.SplitResult;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则匹配工具类
 *
 * @author whj
 * @version 1.0
 * @date 2021-01-18 11:07
 */
public class RegularMatchUtil {
    public static SplitResult splitToTown(String address, Set<String> countySet) {
        SplitResult splitResult = new SplitResult();
        String regex = "(?<privince>^(浙江省)|^(浙江))?(?<city>温州市|温州)" +
                "?(?<county>" + getCountyRegex(countySet) + ")" +
                "?(?<rest>.*)";
        Matcher m = Pattern.compile(regex).matcher(address);
        if (m.find()) {
            splitResult.setCounty(m.group("county"));
            splitResult.setRest(MyStringUtil.emptyToNull(m.group("rest")));
        }
        return splitResult;
    }

    /**
     * 使用正则匹配未匹配到的乡镇
     *
     * @param splitResult
     */
    public static void splitTown(SplitResult splitResult) {
        String regex = "(?<town>^([^镇]+?镇))?(?<rest>.*)";
        String rest = splitResult.getRest();
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        if (matcher.find()) {
            splitResult.setRemark(matcher.group("town"));
            splitResult.setRest(MyStringUtil.emptyToNull(matcher.group("rest")));
        }
    }

    /**
     * 使用正则匹配园区
     *
     * @param splitResult
     */
    public static void splitCommunity(SplitResult splitResult) {
        String regex = "(?<community>^([^园区]+?园区))?(?<rest>.*)";
        String rest = splitResult.getRest();
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        if (matcher.find()) {
            splitResult.setCommunity(matcher.group("community"));
            splitResult.setRest(MyStringUtil.emptyToNull(matcher.group("rest")));
        }
    }

    /**
     * 使用正则匹配自然村
     *
     * @param splitResult
     */
    public static void splitVillage(SplitResult splitResult) {
        String regex = "(?<village>[^村]+村)?(?<rest>.*)";
        String rest = splitResult.getRest();
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        if (matcher.find()) {
            splitResult.setVillage(matcher.group("village"));
            splitResult.setRest(MyStringUtil.emptyToNull(matcher.group("rest")));
        }
    }

    /**
     * 使用正则匹配街路巷弄名
     *
     * @param splitResult
     */
    public static void splitStreet(SplitResult splitResult) {
       /* String regex = "(?<street>[^街]+?街|[^路]+?路|[^巷]+?巷|[^弄]+?弄|[^道]+?道)?(?<nong>[^弄]+?弄|[^巷]+?巷|[^路]+?路)" +
                "?(?<door>([0-9]+.*[0-9]+号)|(-[0-9]+号)|([0-9]+号)|[0-9]+.*号" +
                "|([0-9]+.*[0-9]+)|(-[0-9]+)|([0-9]+))?(?<rest>.*)";*/
        String regex = "(?<street>[^街]+?街|[^路]+?路|[^巷]+?巷|[^弄]+?弄|[^道]+?道)?(?<nong>[^弄]+?弄|[^巷]+?巷|[^路]+?路)" +
                "?(?<door>[0-9a-zA-Z_－-]+?号" +
                "|[0-9a-zA-Z_－-]+(?<!室)$)?(?<rest>.*)";
        String rest = splitResult.getRest();
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        if (matcher.find()) {
            String street = matcher.group("street");
            String nong = matcher.group("nong");
            if (street != null && nong != null) {
                splitResult.setStreet(street + nong);
            } else if (street != null) {
                splitResult.setStreet(street);
            } else if (nong != null) {
                splitResult.setStreet(nong);
            }
            splitResult.setDoor(matcher.group("door"));
            splitResult.setRest(MyStringUtil.emptyToNull(matcher.group("rest")));
        }
    }

    /**
     * 使用正则匹配门牌号
     *
     * @param splitResult
     */
    public static void splitDoor(SplitResult splitResult) {
        String regex = "(?<door>[0-9a-zA-Z_－-]+?号)?(?<rest>.*)";
        String rest = splitResult.getRest();
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        if (matcher.find()) {
            splitResult.setDoor(matcher.group("door"));
            splitResult.setRest(MyStringUtil.emptyToNull(matcher.group("rest")));
        }
    }

    public static void splitUnit(SplitResult splitResult) {
        //|[^区]+区
        String regex = "(?<subarea>[^组]+?组)" +
                "?(?<building>[^号楼]+号楼|[^座]+座|[^栋]+栋|[^幢]+幢|[^楼]+楼)" +
                "?(?<unit>[^单元]+单元)" +
                "?(?<floor>[0-9a-zA-Z_－-]+?[层楼])" +
                "?(?<door>[^间]+间|[^号]+号|[^室]+室|[0-9a-zA-Z_－-]+)?(?<rest>.*)";
        String rest = splitResult.getRest();
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        if (matcher.find()) {
            splitResult.setBuildingNum(matcher.group("building"));
            splitResult.setUnit(matcher.group("unit"));
            splitResult.setFloor(matcher.group("floor"));
            if (splitResult.getYardName() == null && splitResult.getDoor() == null) {
                splitResult.setDoor(matcher.group("door"));
            } else {
                splitResult.setHomeNum(matcher.group("door"));
            }
            String subarea = matcher.group("subarea");
            if (subarea != null && splitResult.getSubareaName() == null) {
                splitResult.setSubareaName(subarea);
            }

            splitResult.setRest(MyStringUtil.emptyToNull(matcher.group("rest")));
        }
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        //String regex = "(?<street>[^街]+街|[^路]+路|[^巷]+巷|[^弄]+弄?)?(?<rest>.*)";
        /*String regex = "(?<street>[^街]+?街|[^路]+?路|[^巷]+?巷|[^弄]+?弄|[^道]+?道)?(?<nong>[^弄]+?弄|[^巷]+?巷|[^路]+?路)" +
                "?(?<door>[0-9a-zA-Z_-]+?号" +
                "|[0-9a-zA-Z_-]+)?(?<rest>.*)";*/
        String regex = "(?<street>[^街]+?街|[^路]+?路|[^巷]+?巷|[^弄]+?弄|[^道]+?道)?(?<nong>[^弄]+?弄|[^巷]+?巷|[^路]+?路)" +
                "?(?<door>[0-9a-zA-Z_－-]+?号" +
                "|[0-9a-zA-Z_－-]+(?<!室)$)?(?<rest>.*)";
        String rest = "文苑路GA101室";
        Matcher matcher = Pattern.compile(regex).matcher(rest);
        String road = null;
        if (matcher.find()) {
            //road = matcher.group("street");
            //rest = matcher.group("rest");
            System.out.println(matcher.group("street"));
            System.out.println("---------------");
            System.out.println(matcher.group("door"));
            System.out.println(matcher.group("rest"));
        }
        /*if(road != null){
            //regex = "(?<door>([0-9]+号)|([0-9]+-[0-9]+号)|([0-9]+-[0-9]+)|([0-9]+)?)?(?<rest>.*)";
            regex = "(?<door>([0-9]+.*[0-9]+号)|(-[0-9]+号)|([0-9]+.*[0-9]+)|(-[0-9]+)?)?(?<rest>.*)";
            matcher = Pattern.compile(regex).matcher(rest);
            if(matcher.find()){
                System.out.println(road);
                System.out.println(matcher.group("door"));
                System.out.println("-------------------------");
                System.out.println(matcher.group("rest"));
            }
        }*/
    }

    /**
     * 使用正则匹配剩下部分:楼牌号,院内门牌号
     *
     * @param splitResult
     */
    public static void splitRest(SplitResult splitResult) {
        if (splitResult.getStreet() == null) {
            RegularMatchUtil.splitStreet(splitResult);
        } else if (splitResult.getStreet() != null && splitResult.getRest() != null) {
            RegularMatchUtil.splitDoor(splitResult);
        }
        if (splitResult.getCommunity() == null && splitResult.getRest() != null) {
            RegularMatchUtil.splitCommunity(splitResult);
        }
        if (splitResult.getVillage() == null && splitResult.getRest() != null) {
            RegularMatchUtil.splitVillage(splitResult);
        }
        if (splitResult.getYardName() == null && splitResult.getDoor() == null && splitResult.getRest() != null) {
            RegularMatchUtil.splitDoor(splitResult);
        }
        if (splitResult.getRest() != null) {
            RegularMatchUtil.splitUnit(splitResult);
        }

    }

    /**
     * 拼凑所有区县正则串
     *
     * @param countySet
     * @return
     */
    public static String getCountyRegex(Set<String> countySet) {
        StringBuilder builder = new StringBuilder();
        for (String item : countySet) {
            builder.append(item).append("|");
        }
        return builder.deleteCharAt(builder.length() - 1).toString();
    }


}
